package com.aretias.jkholdings.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aretias.jkholdings.model.Board;
import com.aretias.jkholdings.model.BoardParam;
import com.aretias.jkholdings.model.BoardReply;
import com.aretias.jkholdings.model.BoardReplyParam;
import com.aretias.jkholdings.model.Category;
import com.aretias.jkholdings.service.BoardService;
import com.aretias.jkholdings.service.CategoryService;

@Controller
public class BoardController extends BaseController{
	
	@Autowired
	private BoardService boardService;
	@Autowired
	private CategoryService categoryService;

	private static final String QNA_BOARD_TYPE = "qna";
	private static final String NOTICE_BOARD_TYPE = "notice";

	@RequestMapping("/board/list")
	public ModelAndView getBoardList(BoardParam param, HttpSession session){
		
		ModelAndView mav = new ModelAndView("board/list");
		
		Category category = new Category();
		category.setParentCategoryId(0);
		List<Category> categoryList = categoryService.getChild(category);
		List<Board> boardList = boardService.getBoardList(param);
		
		// 페이징관련된 정보를 만든다.
		param.setLimitParam();
		setPageInfo(mav, BoardParam.PAGE_SIZE, BoardParam.PAGE_BLOCK_SIZE, boardService.getBoardListCount(param), param.getPageNumber());

		mav.addObject("categoryList", categoryList);
		mav.addObject("boardType", param.getBoardType());
		mav.addObject("boardList", boardList);
		mav.addObject("session", getUserForSession(session));
		return mav;
	}
	
	@RequestMapping("/qna.do")
	public ModelAndView getQnaList(BoardParam param, HttpSession session){
		
		ModelAndView mav = new ModelAndView("board/list");
		
		param.setBoardType(BoardController.QNA_BOARD_TYPE);
		Category category = new Category();
		category.setParentCategoryId(0);
		List<Category> categoryList = categoryService.getChild(category);
		
		List<Board> boardList = boardService.getBoardList(param);
		
		// 페이징관련된 정보를 만든다.
		param.setLimitParam();
		setPageInfo(mav, BoardParam.PAGE_SIZE, BoardParam.PAGE_BLOCK_SIZE, boardService.getBoardListCount(param), param.getPageNumber());
		
		mav.addObject("categoryList", categoryList);
		mav.addObject("boardList", boardList);
		mav.addObject("session", getUserForSession(session));
		return mav;
	}
	
	@RequestMapping("/notice.do")
	public ModelAndView getNoticeList(BoardParam param, HttpSession session){
		
		ModelAndView mav = new ModelAndView("board/list");
		
		param.setBoardType(BoardController.NOTICE_BOARD_TYPE);
		Category category = new Category();
		category.setParentCategoryId(0);
		
		List<Category> categoryList = categoryService.getChild(category);
		List<Board> boardList = boardService.getBoardList(param);
		boardService.getBoardListCount(param);
		
		// 페이징관련된 정보를 만든다.
		param.setLimitParam();
		setPageInfo(mav, BoardParam.PAGE_SIZE, BoardParam.PAGE_BLOCK_SIZE, boardService.getBoardListCount(param), param.getPageNumber());
		
		mav.addObject("categoryList", categoryList);
		mav.addObject("boardList", boardList);
		mav.addObject("session", getUserForSession(session));
		return mav;
	}
	
	@RequestMapping("/board/view/{boardType}/{boardId}.do")
	public ModelAndView getArticle(@PathVariable int boardId, @PathVariable String boardType) {
		
		BoardParam param = new BoardParam();
		BoardReplyParam rparam = new BoardReplyParam();
		Category category = new Category();
		
		category.setParentCategoryId(0);
		param.setBoardType(boardType);
		param.setBoardId(boardId);
		rparam.setBoardId(boardId);
		
		Board board = boardService.getBoard(param);
		List<BoardReply> boardReplyList = boardService.getBoardReplyList(rparam);
		List<Category> categoryList = categoryService.getChild(category);
		
		ModelAndView mav = new ModelAndView("board/view");
		mav.addObject("board", board);
		mav.addObject("boardReplyList", boardReplyList);
		mav.addObject("boardType", boardType);
		mav.addObject("categoryList", categoryList);
		
		return mav;
	}
	
	@RequestMapping("/board/form")
	public ModelAndView getForm(BoardParam param) {
		
		Category category = new Category();
		category.setParentCategoryId(0);
		
		List<Category> categoryList = categoryService.getChild(category);
		
		ModelAndView mav = new ModelAndView("board/form");
		mav.addObject("board", param);
		mav.addObject("categoryList", categoryList);
		return mav;
	}
	
	@RequestMapping("/board/modifyForm")
	public ModelAndView getModifyForm(BoardParam param) {
		
		Category category = new Category();
		category.setParentCategoryId(0);
		
		List<Category> categoryList = categoryService.getChild(category);
		Board board = boardService.getBoard(param);
		
		ModelAndView mav = new ModelAndView("board/modifyForm");
		
		mav.addObject("board", board);
		mav.addObject("categoryList", categoryList);
		return mav;
	}
	
	@RequestMapping(value = "/board/form/update" , method = RequestMethod.POST)
	public ModelAndView modifyBoard(BoardParam param) throws SQLException, NullPointerException, Exception {
		
		boardService.modifyBoard(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/board/view/"+ param.getBoardType() + "/" +Integer.toString(param.getBoardId())+".do");
		return mav;
	}
	
	@RequestMapping(value = "/board/form/add.do" , method = RequestMethod.POST)
	public ModelAndView addBoard(BoardParam param) throws SQLException, NullPointerException, Exception {
		int boardId = boardService.addBoard(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/board/view/"+ param.getBoardType() + "/" +Integer.toString(boardId)+".do");
		return mav;
	}
	
	@RequestMapping(value = "/board/form/delete" , method = RequestMethod.POST)
	public ModelAndView removeBoard(BoardParam param){
		String viewName = "";
		if(QNA_BOARD_TYPE.equals(param.getBoardType().toLowerCase())){
			viewName = "redirect:/qna.do";
		}else if(NOTICE_BOARD_TYPE.equals(param.getBoardType().toLowerCase())){
			viewName = "redirect:/notice.do";
		}else{
			viewName = "redirect:/home.do";
		}
		
		boardService.removeBoard(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewName);
		return mav;
	}
	
	@RequestMapping(value = "/boardReply/form/add.do" , method = RequestMethod.POST)
	public ModelAndView addReplyForm(BoardReplyParam param) throws SQLException, NullPointerException, Exception {
		int boardId = param.getBoardId();
		boardService.addBoardReply(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/board/view/"+ param.getBoardType() + "/" +Integer.toString(boardId)+".do");
		return mav;
	}
	
	@RequestMapping(value = "/boardReply/form/delete.do" , method = RequestMethod.POST)
	public ModelAndView removeReplyForm(BoardReplyParam param){
		boardService.removeBoardReply(param);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("redirect:/board/view/"+ param.getBoardType() + "/" +Integer.toString(param.getBoardId())+".do");
		return mav;
	}
	
	/**
	 * 게시판 비밀번호를 확인하는 ajax
	 * @param param
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/board/checkPassWord")
	public void getCheckPassWord(BoardParam param, HttpServletResponse response) throws IOException{
		String status = "false";
		Board board = boardService.getBoard(param);
		
		if(board != null){
			status = "true";
		}else{
			status = "false";
		}
		response.setContentType( "text/plain; charset=UTF-8" );
		response.getOutputStream().write( status.getBytes() );
	}
	
}